package com.zpl.order.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zpl.core.Result;
import com.zpl.order.entity.Cart;
import com.zpl.order.entity.Goods;
import com.zpl.order.entity.Order;
import com.zpl.order.entity.dto.CartKeyWordDTO;
import com.zpl.order.entity.vo.CartVO;
import com.zpl.order.feign.GoodsFeign;
import com.zpl.order.service.CartService;
import com.zpl.order.service.OrderDetailsService;
import com.zpl.order.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/cart")
@Api(value = "CartController", tags = {"购物车管理"})
public class CartController {
    @Resource
    private CartService cartService;

    @Resource
    private OrderService orderService;

    @Resource
    private OrderDetailsService orderDetailsService;

    @Autowired
    private GoodsFeign goodsFeign;


    @ApiOperation(value = "1、添加购物车信息", notes = "添加购物车信息", httpMethod = "POST")
    @ApiImplicitParam(name = "cart", value = "购物车信息", dataType = "Cart",required = true)
    @PostMapping
    public Result insert(@RequestBody Cart cart) {

//        redisCacheTemplate.opsForValue().set("user:", user);
//        User user1 = (User) redisCacheTemplate.opsForValue().get("user:");


        if (cartService.insert(cart)) {
            return Result.success("购物车添加成功");
        }
        return Result.fail("系统错误");
    }

    @ApiOperation(value = "2、删除购物车信息", notes = "按id删除购物车信息", httpMethod = "DELETE")
    @ApiImplicitParam(name = "id", value = "id", dataType = "int",required = true)
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id) {
        if (cartService.delete(id)) {
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }

    @ApiOperation(value = "3、删除已选中购物车信息", notes = "按会员id删除购物车信息", httpMethod = "DELETE")
//    @ApiImplicitParam(name = "cart", value = "购物车信息", dataType = "Cart",required = true)
    @DeleteMapping("/deleteByChecked")
    public Result deleteByChecked(@RequestParam("memberId") Long memberId) {

//        Long memberId = 0L;//等其他模块完成后再加

//        List<Cart> list = cartService.selectByChecked(memberId);
//
//        order.setMemberId(memberId);
//        if (orderService.insert(order)==false) {
//            return Result.fail("订单生成失败");
//        }

        //此处需要商品模块获得信息，添加到订单细节表


        if (cartService.deleteByChecked(memberId)) {
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }

    @ApiOperation(value = "4、修改购物车信息", notes = "按id修改购物车信息", httpMethod = "PUT")
    @ApiImplicitParam(name = "cart", value = "购物车信息", dataType = "Cart",required = true)
    @PutMapping
    public Result update(@RequestBody Cart cart) {
        if (cartService.update(cart)) {
            return Result.success("修改成功");
        }
        return Result.fail("修改失败");
    }

    @ApiOperation(value = "5、获取购物车信息", notes = "按会员id获取购物车信息", httpMethod = "POST")
    @ApiImplicitParam(name = "cartKeyWordDTO", value = "购物车分页信息", dataType = "CartKeyWordDTO",required = true)
    @PostMapping("/list")
    public Result getCartByMemberId(@RequestBody CartKeyWordDTO cartKeyWordDTO) {
        IPage<Cart> page = cartService.getCartByMemberId(cartKeyWordDTO);
        List<CartVO> list =new ArrayList<>();
        for(int i=0;i<page.getRecords().size();i++){
            CartVO cartVO = new CartVO();
            cartVO.setId(page.getRecords().get(i).getId());
            cartVO.setChecked(page.getRecords().get(i).getChecked());
            cartVO.setGoodId(page.getRecords().get(i).getGoodId());
            cartVO.setGoodNum(page.getRecords().get(i).getGoodNum());
            cartVO.setMemberId(page.getRecords().get(i).getMemberId());
            cartVO.setCreateDate(page.getRecords().get(i).getCreateDate());
            cartVO.setOperatorId(page.getRecords().get(i).getOperatorId());
            cartVO.setUpdateDate(page.getRecords().get(i).getUpdateDate());
            Goods goods = goodsFeign.getGoodFeign(page.getRecords().get(i).getGoodId());
            cartVO.setTitle(goods.getTitle());
            cartVO.setOriginalPrice(goods.getOriginalPrice());
            cartVO.setNowPrice(goods.getNowPrice());
            cartVO.setMainImage(goods.getMainImage());
            list.add(cartVO);
        }
        IPage<CartVO> iPage=new Page<>();
        iPage.setCurrent(page.getCurrent());
        iPage.setPages(page.getPages());
        iPage.setSize(page.getSize());
        iPage.setTotal(page.getTotal());
        iPage.setRecords(list);
        return Result.success(iPage);
    }

    @ApiOperation(value = "6、查询购物车信息", notes = "查询购物车选中信息", httpMethod = "POST")
    @ApiImplicitParam(name = "memberId", value = "memberId", dataType = "Long",required = true)
    @PostMapping("/getChecked")
    public Result getByChecked(@RequestParam("memberId") Long memberId){
        List<Cart> carts=cartService.selectByChecked(memberId);
        List<CartVO> list =new ArrayList<>();
        for(int i=0;i<carts.size();i++){
            CartVO cartVO=new CartVO();
            cartVO.setId(carts.get(i).getId());
            cartVO.setChecked(carts.get(i).getChecked());
            cartVO.setGoodId(carts.get(i).getGoodId());
            cartVO.setGoodNum(carts.get(i).getGoodNum());
            cartVO.setMemberId(carts.get(i).getMemberId());
            cartVO.setCreateDate(carts.get(i).getCreateDate());
            cartVO.setOperatorId(carts.get(i).getOperatorId());
            cartVO.setUpdateDate(carts.get(i).getUpdateDate());
            Goods goods=goodsFeign.getGoodFeign(carts.get(i).getGoodId());
            cartVO.setTitle(goods.getTitle());
            cartVO.setOriginalPrice(goods.getOriginalPrice());
            cartVO.setNowPrice(goods.getNowPrice());
            cartVO.setMainImage(goods.getMainImage());
            list.add(cartVO);
        }
        return Result.success(list);
    }

}
